package com.deer.wms.base.system.service.bill.impl;


import com.deer.wms.base.system.dao.bill.BillInDetailMapper;
import com.deer.wms.base.system.dao.bill.MaterialDetailMapper;
import com.deer.wms.base.system.model.bill.*;
import com.deer.wms.base.system.model.task.TaskInfo;
import com.deer.wms.base.system.model.zh.WorkTasks;
import com.deer.wms.common.core.service.AbstractService;
import com.deer.wms.common.core.text.Convert;
import com.deer.wms.base.system.dao.bill.BillInMasterMapper;
import com.deer.wms.base.system.model.box.BoxItem;
import com.deer.wms.base.system.service.bill.IBillInMasterService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


/**
 * 入库单 服务层实现
 * 
 * @author guo
 * @date 2019-05-13
 */
@Service
@Transactional
public class BillInMasterServiceImpl  extends AbstractService<BillInMaster, Integer> implements IBillInMasterService
{
	@Autowired
	private BillInMasterMapper billInMasterMapper;

	@Autowired
	private SqlSessionFactory sqlSessionFactory;
	/**
     * 查询入库单信息
     * 
     * @param billId 入库单ID
     * @return 入库单信息
     */
    @Override
	public BillInMasterDto selectBillInMasterById(Integer billId)
	{
	    return billInMasterMapper.selectBillInMasterById(billId);
	}

	@Override
	public BillInMaster selectBillInMasterByBillNo(String billNo) {
		return billInMasterMapper.selectBillInMasterByBillNo(billNo);
	}

	@Override
	public int auditFinish(Integer billId) {
		return billInMasterMapper.auditFinish(billId);
	}

	@Override
	public Boolean createInfoByBillNO(BillInMaster billInMaster) {
		 if(billInMasterMapper.createInfoByBillNO(billInMaster) == 1){
		 	return true;
		 }
		 return false;
	}

	@Override
	public List<BillInMaster> getBillInMasterList() {
		return billInMasterMapper.getBillInMasterList();
	}

	@Override
	public int startAssignment(Integer billId) {
		return billInMasterMapper.startAssignment(billId);
	}

	@Override
	public int endAssignment(Integer billId) {
		return billInMasterMapper.endAssignment(billId);
	}

	/**
	 *
	 * 查询物料详情数据(点击物料详情)
	 */
	@Override
	public List<BillInDetail> selectBillInDetail() {


		return billInMasterMapper.selectBillInDetail();
	}


	/**
	 *
	 * 查询组盘(点击组盘信息)
	 */
	@Override
	public List<BoxItem> selectBoxItem(Integer billId) {

		return billInMasterMapper.selectBoxItem(billId);
	}

	/**
	 *
	 * 保存组盘
	 */
	@Override
	public void saveBox(BoxItem boxItem) {

		billInMasterMapper.saveBox(boxItem);

	}





	/**
     * 查询入库单列表
     *
     * @param criteria 入库单信息
     * @return 入库单集合
     */
	@Override
	public List<BillInMasterDto> selectBillInMasterList(BillInMasterCriteria criteria)
	{
	    return billInMasterMapper.selectBillInMasterList(criteria);
	}

	@Override
	public List<BillInMasterDto> findExportList(BillInMasterCriteria criteria) {
		return billInMasterMapper.findExportList(criteria);
	}

	@Override
	public List<BillInMasterDto> findList(BillInMasterCriteria billInMasterCriteria) {
		return billInMasterMapper.findList(billInMasterCriteria);
	}


	@Override
	public int insertBillInMaster(BillInMaster billInMaster) {

	    return billInMasterMapper.insertBillInMaster(billInMaster);
	}
	
	/**
     * 修改入库单
     * 
     * @param billInMaster 入库单信息
     * @return 结果
     */
	@Override

	public int updateBillInMaster(BillInMaster billInMaster)
	{
	    return billInMasterMapper.updateBillInMaster(billInMaster);
	}

	/**
     * 删除入库单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteBillInMasterByIds(String ids)
	{
		return billInMasterMapper.deleteBillInMasterByIds(Convert.toStrArray(ids));
	}

	@Override
	public int deleteBillInDetailByIds(String ids) {
		return billInMasterMapper.deleteBillInDetailByIds(Convert.toStrArray(ids));
	}

	@Override
	public int BillInMasterCount() {
		return billInMasterMapper.BillInMasterCount();
	}

	@Override
	public int BillInMasterCountByState() {
		return billInMasterMapper.BillInMasterCountByState();
	}

	@Override
	public List<Integer> getBillInMasterWeek() {
		return billInMasterMapper.getBillInMasterWeek();
	}

	@Override
	public BillInMasterDto findBillInMasterById(Integer billId) {
		return billInMasterMapper.findBillInMasterById(billId);
	}

	@Override
	public void insertMaterialInfo(String strMaterials) {

		List<MaterialInfo> materialInfoList = new ArrayList<>();
		List<MaterialDetail> materialDetailsList = new ArrayList<>();

		for(int i=0;i<100;i++)
		{
			Random random = new Random();
			MaterialInfo materialInfo = new MaterialInfo(i+"","a","b","c","A");
			materialInfoList.add(materialInfo);

			MaterialDetail materialDetail = new MaterialDetail(i+ "","A",random.nextInt(100000) + "-" + i,"B","100",random.nextInt(100000)+"-" + random.nextInt(300000),"C");
			materialDetailsList.add(materialDetail);
		}
		SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
		BillInMasterMapper billInMasterMapper = sqlSession.getMapper(BillInMasterMapper.class);
		materialInfoList.stream().forEach(materialInfo -> billInMasterMapper.insertMaterialInfo(materialInfo));

		MaterialDetailMapper materialDetailMapper= sqlSession.getMapper(MaterialDetailMapper.class);
		materialDetailsList.stream().forEach(materialDetail -> materialDetailMapper.insertMaterialDetail(materialDetail));

		sqlSession.commit();
		sqlSession.clearCache();
		sqlSession.close();
	}


	@Override
	public List<BillInMaster> selectBillInMaster(BillInMaster billInMaster) {
		return billInMasterMapper.selectBillInMaster(billInMaster);
	}

	@Override
	public BillInMaster findBillInMasterByBillId(Integer billId) {
		return billInMasterMapper.findBillInMasterByBillId(billId);
	}

	@Override
	public List<BillInDetailDto> findBillInDetailById(Integer billId) {
		return billInMasterMapper.findBillInDetailById(billId);
	}

	@Override
	public List<BillInDetailItemDto> findBillInDetailItemById(Integer detailId) {
		return billInMasterMapper.findBillInDetailItemById(detailId);
	}

	@Override
	public List<BillInDetailStreamDto> selectDetailStreamList(BillInDetailStreamDto billInDetailStreamDto) {
		return billInMasterMapper.selectDetailStreamList(billInDetailStreamDto);
	}

	@Override
	public List<BillInMaster> findBillInMasterByItemCode(WorkTasks workTasks) {
		return billInMasterMapper.findBillInMasterByItemCode(workTasks);
	}

	@Override
	public void insertBillInDetail(BillInDetail billInDetail) {
		billInMasterMapper.insertBillInDetail(billInDetail);
	}

	@Override
	public List<BillInDetailDto> selectBillInDetailByTaskNumber(Long taskNumber) {
		return billInMasterMapper.selectBillInDetailByTaskNumber(taskNumber);
	}

	@Override
	public void insertBillInDetailItem(BillInDetailItem billInDetailItem) {
		billInMasterMapper.insertBillInDetailItem(billInDetailItem);
	}

	@Override
	public void saveBillInDetail(BillInDetail billInDetail) {
		billInMasterMapper.saveBillInDetail(billInDetail);
	}

	@Override
	public void saveBillInDetailHeight(BillInDetail billInDetail) {
		billInMasterMapper.saveBillInDetailHeight(billInDetail);
	}

	@Override
	public List<BillInMaster> selectBillInMasterPda(BillInMaster billInMaster) {
		return billInMasterMapper.selectBillInMasterPda(billInMaster);
	}

	@Override
	public void updateSapNoBySapId(BillInSap billInSap) {
		billInMasterMapper.updateSapNoBySapId(billInSap);
	}

	@Override
	public void deleteBillInDetailItemById(Integer billInDetailId) {
		billInMasterMapper.deleteBillInDetailItemById(billInDetailId);
	}

	@Override
	public void updateBillInDetail(BillInDetail billInDetail) {
		billInMasterMapper.updateBillInDetail(billInDetail);
	}

	@Override
	public BillInDetailDto selectHeightByTaskNumber(Long taskNumber) {
		return billInMasterMapper.selectHeightByTaskNumber(taskNumber);
	}

	@Override
	public void saveInDetailId(Integer id, Integer billInDetailId) {
		billInMasterMapper.saveInDetailId(id,billInDetailId);
	}

	@Override
	public BillInDetailStreamDto getDetailSum(BillInDetailStreamDto detailAll) {
		return billInMasterMapper.getDetailSum(detailAll);
	}


}
